import employeeApi from 'api/employee'

// state
let state = {
  list: {
    params: {}, // 获取参数
    data: {} // 分页数据
  },
  create: [], // 创建角色
  role_list: [], // 获取组织职务角色权限列表
  edit: [], // 获取当前修改角色的信息
  organize_service: [], // 获取roleId认证服务
  authorize_service_list: [], // 获取查询认证服务列表
  ip_res_list: []  // 获取现有权限列表
}

// getters
const getters = {
  employee_list: state => state.list,
  employee_create: state => state.create,
  employee_role_list: state => state.role_list,
  employee_edit: state => state.edit,
  employee_organize_service: state => state.organize_service,
  employee_authorize_service_list: state => state.authorize_service_list,
  employee_ip_rest_list: state => state.ip_res_list
}

// actions
const actions = {
  employee_list ({ commit, state }, params) {
    employeeApi.list(params).then((json) => {
      commit('EMPLOYEE_LIST', {
        params: params,
        data: json.data
      })
    })
  },
  employee_create ({ commit, state }, params) {
    employeeApi.create(params).then((json) => {
      commit('EMPLOYEE_CREATE', {
        obj: params,
        data: json.data
      })
    })
  },
  employee_role_list ({ commit, state }, params) {
    employeeApi.roleList(params).then((json) => {
      commit('EMPLOYEE_ROLE_LIST', {
        obj: params,
        data: json.data
      })
    })
  },
  employee_edit ({ commit, state }, params) {
    employeeApi.edit(params).then((json) => {
      commit('EMPLOYEE_EDIT', {
        obj: params,
        data: json.data
      })
    })
  },
  employee_organize_service ({ commit, state }, params) {
    employeeApi.role(params).then((json) => {
      commit('EMPLOYEE_ORGANIZE_SERVICE', {
        obj: params,
        data: json.data
      })
    })
  },
  employee_authorize_service_list ({ commit, state }, params) {
    employeeApi.authServiceList(params).then((json) => {
      commit('EMPLOYEE_SERVICE_LIST', {
        obj: params,
        data: json.data
      })
    })
  },
  employee_ip_rest_list ({ commit, state }, params) {
    employeeApi.ipResList(params).then((json) => {
      commit('EMPLOYEE_IP_RES_LIST', {
        obj: params,
        data: json.data
      })
    })
  }
}
// mutations
const mutations = {
  EMPLOYEE_LIST (state, { data, params }) {
    state.list = data
  },
  EMPLOYEE_CREATE (state, { data, params }) {
    state.create = data
  },
  EMPLOYEE_ROLE_LIST (state, { data, params }) {
    state.role_list = Object.assign({}, state.role_list, data)
    // state.role_list = data
  },
  EMPLOYEE_EDIT (state, { data, params }) {
    state.edit = data
  },
  EMPLOYEE_ORGANIZE_SERVICE (state, { data, params }) {
    state.organize_service = data
  },
  EMPLOYEE_SERVICE_LIST (state, { data, params }) {
    state.authorize_service_list = data
  },
  EMPLOYEE_IP_RES_LIST (state, { data, params }) {
    state.ip_res_list = Object.assign({}, state.ip_res_list, data)
    // state.ip_res_list = data
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
